//
//  XMStatus.m
//  XMWeibo
//
//  Created by chi on 14-8-23.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMStatus.h"
#import "NSDate+XM.h"
#import "XMPhoto.h"


@implementation XMStatus

- (NSDictionary*)objectClassInArray
{
    return @{@"pic_urls": [XMPhoto class]};
}



- (NSString *)statusTime
{
    //Tue Aug 26 00:09:34 +0800 2014
//    _created_at = @"Tue Aug 25 00:09:34 +0800 2014";
    NSDateFormatter *ftm = [[NSDateFormatter alloc]init];
    ftm.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    ftm.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    
    NSDate *createDate = [ftm dateFromString:_created_at];
    
    
    if (createDate.isToday) {
        NSDateComponents *cpms = [createDate deltaToNow];
        if (cpms.minute < 1) {
            return @"刚刚";
        }
        else if (cpms.hour < 1)
        {
            return [NSString stringWithFormat:@"%d分钟前", cpms.minute];
        }
        else {
            ftm.dateFormat = @"HH:mm";
            return [ftm stringFromDate:createDate];
        }
    }
    else if (createDate.isYesterday)
    {
        ftm.dateFormat = @"昨天 HH:mm";
        return [ftm stringFromDate:createDate];
    }
    else if (createDate.isThisYear)
    {
        ftm.dateFormat = @"MM-dd HH:mm";
        return [ftm stringFromDate:createDate];
    }
    else {
        ftm.dateFormat = @"yyyy-MM-dd HH:mm";
        return [ftm stringFromDate:createDate];
    }
    
}


- (void)setSource:(NSString *)source
{
    NSRange range = [source rangeOfString:@">"];
    if (range.location != NSNotFound) {
        NSUInteger startIndex = range.location + 1;

        NSUInteger length = [source rangeOfString:@"</"].location - startIndex;
        
        _source = [NSString stringWithFormat:@"来自%@", [source substringWithRange:NSMakeRange(startIndex, length)]];
    }
    else {
        _source = source;
    }
}

//- (id)initWithCoder:(NSCoder *)aDecoder
//{
//    if (self = [super init]) {
//        [self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) {
//            ivar.value = [aDecoder decodeObjectForKey:ivar.propertyName];
//        }];
//    }
//    
//    return self;
//}
//
//- (void)encodeWithCoder:(NSCoder *)aCoder
//{
//    [self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) {
//        [aCoder encodeObject:ivar.value forKey:ivar.propertyName];
//    }];
//}

MJCodingImplementation

@end
